package com.os.osframe.scaffold.create;

import com.os.osframe.scaffold.domain.ScaffoldXmlTable;
import com.os.osframe.scaffold.util.PropertyConfigurerLoader;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * Created by wangdc on 2015-4-27.
 */
public class CreateJsAppCode extends BaseCreate implements ICreateCode {
    public CreateJsAppCode(){

    }

    protected List<ScaffoldXmlTable> scaffoldXmlTableList;
    public CreateJsAppCode(List<ScaffoldXmlTable> scaffoldXmlTableList){
        this.scaffoldXmlTableList=scaffoldXmlTableList;
    }
    public CreateJsAppCode(ScaffoldXmlTable scaffoldXmlTable){
        super(scaffoldXmlTable);
    }
    public void create(String packages)throws Exception{
        System.out.println("=====开始生成jsp页面=====");
        //String path = replaceStr(new File("").getAbsolutePath(), "\\", "/");//获取项目的根路径，这里获取到的是softdp的路径
        String basePath = (String) getPropertyConfigurer().getProperty("osframe.file.path");//获取属性配置的服务器路径
        if (basePath.substring(basePath.length() - 1).equals("/")) {
            basePath = basePath.substring(basePath.length() - 1);
        }
        //System.out.println(path);//E:/java/intelijwork/softdp
        String targetFilePath=basePath;
        targetFilePath+="/auto/code/"+packages.replaceAll("\\.","/")+"/js/resource";
        //System.out.println(targetFilePath);
        String tempPath = replaceStr(new File(this.getClass().getResource("").getPath()).getAbsolutePath(), "\\", "/");//获取当前类所在路径
        if(tempPath.indexOf("create")>-1){
            tempPath=tempPath.substring(0,tempPath.length()-7);
        }
        tempPath+="/jsapp";
        //System.out.println(tempPath);
        //xmlObjList= ReadXmlObj.getDataInfoByModule(module, packages);//获取列表对象
        createFile(tempPath,targetFilePath);
    }

    /**
     *
     * @param srcText
     * @return
     * @throws Exception
     */
    @Override
    protected String replaceCode(String srcText) throws Exception{

        if(scaffoldXmlTableList==null || scaffoldXmlTableList.isEmpty()){
            return srcText;
        }
        srcText=replaceApp(srcText,scaffoldXmlTableList);

        return srcText;
    }
    /**
     * 替换编辑页面
     * @param srcText
     * @param scaffoldXmlTableList
     * @return
     * @throws Exception
     */
    protected String replaceApp(String srcText,List<ScaffoldXmlTable> scaffoldXmlTableList) throws Exception{

        StringBuffer sb=new StringBuffer();
        int colNum=0;
        for(ScaffoldXmlTable scaffoldXmlTable : scaffoldXmlTableList){
            if(colNum==0){
                sb.append("\r\n").append("            ").append("$urlRouterProvider.otherwise('/"+scaffoldXmlTable.getJavaName()+"List');");
                sb.append("\r\n").append("            ").append("$stateProvider");
            }
            //列表
            sb.append("\r\n").append("            ").append(".state('"+scaffoldXmlTable.getJavaName()+"List', {");

            sb.append("\r\n").append("                ").append("url: '/"+scaffoldXmlTable.getJavaName()+"List',");
            sb.append("\r\n").append("                ").append("views:{");
            sb.append("\r\n").append("                    ").append("\"\":{");
            sb.append("\r\n").append("                        ").append("controller: '"+scaffoldXmlTable.getJavaName()+"ListCtrl',");
            sb.append("\r\n").append("                        ").append("cache:'false',");

            sb.append("\r\n").append("                        ").append("templateUrl:function($routeParams){\n" +
                    "                            var url= G.path.moduleTemplatePath+'/"+scaffoldXmlTable.getJavaName()+"/list.html';\n" +
                    "                            url=global.setParam(url,'module',$routeParams.module);\n" +
                    "                            url=global.setParam(url,'pageNo',$routeParams.pageNo);\n" +
                    "                            url=global.setParam(url,'searchKey',$routeParams.searchKey);\n" +
                    "                            return url;\n" +
                    "                        },");
            sb.append("\r\n").append("                        ").append("controllerUrl: G.path.moduleResPath+'/"+scaffoldXmlTable.getJavaName()+"/"+scaffoldXmlTable.getJavaName()+"ListCtrl.js'");
            sb.append("\r\n").append("                    ").append("}");
            sb.append("\r\n").append("                ").append("}");
            sb.append("\r\n").append("            ").append("})");
            //编辑
            sb.append("\r\n").append("            ").append(".state('"+scaffoldXmlTable.getJavaName()+"Edit', {");
            sb.append("\r\n").append("                ").append("url: '/"+scaffoldXmlTable.getJavaName()+"Edit?id',");
            sb.append("\r\n").append("                ").append("views:{");
            sb.append("\r\n").append("                    ").append("\"\":{");
            sb.append("\r\n").append("                        ").append("controller: '"+scaffoldXmlTable.getJavaName()+"EditCtrl',");
            sb.append("\r\n").append("                        ").append("cache:'false',");
            sb.append("\r\n").append("                        ").append("templateUrl: function($routeParams){return G.path.moduleTemplatePath+'/"+scaffoldXmlTable.getJavaName()+"/edit.html?id='+$routeParams.id;},");
            sb.append("\r\n").append("                        ").append("controllerUrl: G.path.moduleResPath+'/"+scaffoldXmlTable.getJavaName()+"/"+scaffoldXmlTable.getJavaName()+"EditCtrl.js'");
            sb.append("\r\n").append("                    ").append("}");
            sb.append("\r\n").append("                ").append("}");
            sb.append("\r\n").append("            ").append("})");
            //查看
            sb.append("\r\n").append("            ").append(".state('"+scaffoldXmlTable.getJavaName()+"Look', {");
            sb.append("\r\n").append("                ").append("url: '/"+scaffoldXmlTable.getJavaName()+"Look?id',");
            sb.append("\r\n").append("                ").append("views:{");
            sb.append("\r\n").append("                    ").append("\"\":{");
            sb.append("\r\n").append("                        ").append("controller: '"+scaffoldXmlTable.getJavaName()+"LookCtrl',");
            sb.append("\r\n").append("                        ").append("cache:'false',");
            sb.append("\r\n").append("                        ").append("templateUrl: function($routeParams){return G.path.moduleTemplatePath+'/"+scaffoldXmlTable.getJavaName()+"/look.html?id='+$routeParams.id;},");
            sb.append("\r\n").append("                        ").append("controllerUrl: G.path.moduleResPath+'/"+scaffoldXmlTable.getJavaName()+"/"+scaffoldXmlTable.getJavaName()+"LookCtrl.js'");
            sb.append("\r\n").append("                    ").append("}");
            sb.append("\r\n").append("                ").append("}");
            sb.append("\r\n").append("            ").append("})");
            //新建
            sb.append("\r\n").append("            ").append(".state('"+scaffoldXmlTable.getJavaName()+"Add', {");
            sb.append("\r\n").append("                ").append("url: '/"+scaffoldXmlTable.getJavaName()+"Add',");
            sb.append("\r\n").append("                ").append("views:{");
            sb.append("\r\n").append("                    ").append("\"\":{");
            sb.append("\r\n").append("                        ").append("controller: '"+scaffoldXmlTable.getJavaName()+"EditCtrl',");
            sb.append("\r\n").append("                        ").append("cache:'false',");
            sb.append("\r\n").append("                        ").append("templateUrl: G.path.moduleTemplatePath+'/"+scaffoldXmlTable.getJavaName()+"/add.html',");
            sb.append("\r\n").append("                        ").append("controllerUrl: G.path.moduleResPath+'/"+scaffoldXmlTable.getJavaName()+"/"+scaffoldXmlTable.getJavaName()+"EditCtrl.js'");
            sb.append("\r\n").append("                    ").append("}");
            sb.append("\r\n").append("                ").append("}");
            sb.append("\r\n").append("            ").append("})");
            colNum++;
        }
        if(scaffoldXmlTableList!=null && !scaffoldXmlTableList.isEmpty()){
            sb.append(";");//结尾添加分号
        }

        srcText=replaceStr(srcText,"${appJsConfigCode}",sb.toString());

        //国际化文件路径暂时不使用

        return srcText;
    }
}
